import router from '@/router'
import { removeToken } from '@/utils/auth'
import { getAllRegions } from '@/api/dictionary'
import { getUserInfo, logout, getCountryList, getMenus } from '@/api/user'
import { queryChannelList } from '@/api/operations'
import { getCommonData } from '@/api/dictionary'

// state
const state = {
  cdnUrl: '',
  userInfo: {},
  commonData: {},
  permMap: {},
  userList: [],
  countryList: [],
  channelList: [],
  region1: [],
  region2: [],
  region3: [],
  region4: [],
  leaderList: [],
  regionCascader: [],
  districtCascader: [],
}

// getters
const getters = {
  cdnUrl: state => state.cdnUrl,
  userInfo: state => state.userInfo,
  countryList: state => state.countryList,
  region1: state => state.region1,
  region2: state => state.region2,
  region3: state => state.region3,
  region4: state => state.region4,
  channelList: state => state.channelList,
  commonData: state => state.commonData,
  regionCascader: state => state.regionCascader,
  districtCascader: state => state.districtCascader,
}

// mutations
const mutations = {
  SET_PERM_CDN(state, dir) {
    state.cdnUrl = dir
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo
  },
  SET_USER_LIST(state, userList) {
    state.userList = userList
  },
  SET_COMMON_DATA(state, commonData) {
    state.commonData = commonData
  },
  SET_ALL_REGION(state, data) {
    const { R1: region1, R2: region2, R3: region3, R4: region4 } = data
    state.region1 = region1
    state.region2 = region2
    state.region3 = region3
    state.region4 = region4
  },
  SET_ALL_COUNTRY(state, data) {
    state.countryList = data
  },
  SET_PERM_MAP(state, permMap) {
    state.permMap = permMap
  },
  SET_ALL_CHANNEL(state, data) {
    state.channelList = data
  },
}

// actions
const actions = {
  // 获取用户信息
  async getUserInfo({ commit }) {
    const { data } = await getUserInfo()
    commit('SET_PERM_CDN', process.env.VUE_APP_CND)
    commit('SET_USER_INFO', data)
  },

  // 获取权限菜单
  async getMenus({ commit }) {
    const { data } = await getMenus()
    const { permMap, menuNameMap } = generatePermMap(data)
    commit('SET_PERM_MAP', permMap)
    return permMap
  },
  async getChannel({ commit }) {
    const { data } = await queryChannelList()
    commit('SET_ALL_CHANNEL', data)
  },
  // 获取地区字典
  async getRegions({ commit }) {
    const { data } = await getAllRegions()
    commit('SET_ALL_REGION', data)
    return data
  },
  async getCountry({ commit }) {
    const { data } = await getCountryList()
    commit('SET_ALL_COUNTRY', data?.CountriesList?.list)
  },
  // 获取公共资源
  async getCommonData({ commit }) {
    const { data } = await getCommonData()
    commit('SET_COMMON_DATA', data)
    return data
  },

  // 退出登录
  async logout({ dispatch }) {
    removeToken()
    dispatch('tagsView/delAllViews', null, { root: true })
    logout()
    router.replace({ path: '/login' })
  },
}

// 生产菜单权限map对象
function generatePermMap(menuList) {
  const formatMap = menuList => {
    menuList.forEach(item => {
      const mapKey = item?.Name?.split('/')?.slice(-1)?.toString()
      permMap[mapKey] = item?.Perms ?? []
      menuNameMap[mapKey] = item.Title ?? '未知'
      if (item?.Children?.length) {
        formatMap(item.Children)
      } else {
        item.Children = null
      }
    })
  }
  // 递归设置
  const permMap = {}
  const menuNameMap = {}
  formatMap(menuList)
  return {
    permMap,
    menuNameMap,
  }
}
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
}
